<?php
class QAdminUser
{
	static $_table = 'admins';
	
	static function search($p_strCountry, $p_intGroupId, $p_strUsername)
	{
		$strQuery	= '
			SELECT admin_id, admin_username, group_id, group_leader, admin_isactive, admin_lastlogin, admin_createdate, admin_firstname, admin_lastname, country, email
			FROM '.self::$_table.'
			WHERE 1=1
		';
		if($p_strCountry){
			$strQuery	.= ' AND LOWER(country)="'.strtolower($p_strCountry).'"';
		}
		if($p_intGroupId){
			$strQuery	.= ' AND group_id="'.intval($p_intGroupId).'"';
		}
		if($p_strUsername){
			$strQuery	.= ' AND LOWER(admin_username)="'.strtolower(trim($p_strUsername)).'"';
		}
		$strQuery	.= ' ORDER BY group_id, admin_createdate ASC';
		
		$db 		= Zend_Registry::get('db');
		$resultValue  = $db->fetchAll($strQuery);
		$db->closeConnection();
		
		return $resultValue;
		
	}
	
	static function get_by_id($p_intId)
	{
		$strQuery	= 'SELECT * FROM '.self::$_table.' WHERE admin_id='.intval($p_intId);
		
		$db 		= Zend_Registry::get('db');
		$arrResult	=  $db->fetchAll($strQuery);
		$db->closeConnection();
		
		return isset($arrResult[0]) ? $arrResult[0] : null;
	}
	
	static function get_by_group($p_intGroupId, $strCountry)
	{
		$strQuery	= 'SELECT * FROM '.self::$_table.' WHERE group_id='.intval($p_intGroupId) . ' AND country = "'. $strCountry .'"';
		
		$db 		= Zend_Registry::get('db');
		$arrResult	=  $db->fetchAll($strQuery);
		$db->closeConnection();
		
		return isset($arrResult[0]) ? $arrResult[0] : null;
	}
	
	static function updatePassword($p_userName,$strPassOld, $strPassNew )
	{
		$db 		= Zend_Registry::get('db');
		$strQuery	= 'SELECT * FROM '.self::$_table.' WHERE admin_username='.intval($p_userName);
		$arrResult	=  $db->fetchAll($strQuery);
		
		$arrResult[0]["admin_pwd"] = $strPassNew;
		$resultValue = $db->update(self::$_table, $arrResult[0], 'admin_id='.intval($arrResult[0]['admin_id']));
		$db->closeConnection();
		
		return $resultValue;
	}
	
	public function insert($p_arrParams)
	{
		$db	= Zend_Registry::get('db');
		$resultValue =$db->insert(self::$_table,$p_arrParams);
		$db->closeConnection();
		return $resultValue;
	}
	
	public function update($p_arrParams, $p_intId)
	{
		$db	= Zend_Registry::get('db');
		$resultValue = $db->update(self::$_table, $p_arrParams, 'admin_id='.intval($p_intId));
		$db->closeConnection();
		return $resultValue;
	}
}
?>